<?php

class Authtamer extends Controller {
	
	function Authtamer()
	{
		parent::Controller();
		$this->output->enable_profiler(TRUE);
		$this->load->library('account');
		log_message('debug', "AuthTaMeR Controler Class Initialized");
	}

	function guestMenu()
	{
		$menu[] = anchor('authtamer/login/','Login');
		$menu[] = anchor('account/register/','Register');
		return $menu;
	}

/*****************************************************************\
|               LOGIN
\*****************************************************************/	
	function Login()
	{
		if(isset($_POST['login']))
		{
			log_message('debug', "AuthTaMeR Class Post Login received");
			$this->load->library('validation');
			$rules['alias']	= "trim|xss_clean|required|alpha_numeric";
			$rules['password']	= "trim|xss_clean|required|alpha_numeric";
			$this->validation->set_rules($rules);
			$data['alias'] = 'Alias';
			$data['password'] = 'Password';
			$this->validation->set_fields($data);
			
			if ($this->validation->run() == TRUE)
			{
				$rules['captcha'] = "callback_captcha_check";
				$this->validation->set_rules($rules) ;

				if ($this->validation->run() == TRUE)
				{
					$rules['login'] = "callback_login_check"; # Calling login check
					$this->validation->set_rules($rules) ;
					
					if ($this->validation->run() == FALSE)
					{
						$data['tpltitle'] = 'Login';
						$data['captcha'] = $this->captcha_view();
						$data['tplmenu'] = $this->guestMenu(); 
						$this->load->view('account/login',$data);
					}
				}else{
					$data['tpltitle'] = 'Login';
					$data['captcha'] = $this->captcha_view();
					$data['tplmenu'] = $this->guestMenu(); 
					$this->load->view('account/login',$data);
				}
			}else{
				$data['tpltitle'] = 'Login';
				$data['captcha'] = $this->captcha_view();
				$data['tplmenu'] = $this->guestMenu(); 
				$this->load->view('account/login',$data);
			}
		}else{
			$data['tpltitle'] = 'Login';
			$data['captcha'] = $this->captcha_view();
			$data['tplmenu'] = $this->guestMenu(); 
			$this->load->view('account/login', $data);
		}
	}

	
	/***************************************************\
	*	If post is correct we will login
	\***************************************************/
	function login_check($str)
	{
		log_message('debug', "AuthTaMeR Class check_Login");
		$alias = $this->input->post('alias');
		$pass  = $this->input->post('password');
		$save  = $this->input->post('save');
		$this->load->model('Accountdb');
		$query = $this->Accountdb->Login($alias,$pass);
		if ($query->num_rows() > 0)
		{
			$this->session->set_userdata($query->row());
			$groups = $this->session->userdata('groups');
			$g['groups'] = $groups .',100';
			//100 = fresh login not from cookie 
			$this->session->set_userdata($g);
			if($save == 1)
			{
				$this->auth->addCookie();
			}
			redirect('user', 'refresh');
		}
		else
		{
			$this->validation->set_message('login_check', 'We have no match');
			return FALSE;
		}
	}
	
/*****************************************************************\
|               REGISTER
\*****************************************************************/	

	function Register()
	{
		if(isset($_POST['register']))
		{
			$this->load->library('validation');
			$this->validation->set_error_delimiters('<div class="error">', '</div>');
			$rules['alias']	= "trim|xss_clean|required|alpha_numeric";
			$rules['email'] = "trim|required|valid_email";
			$rules['code'] = "required";
			$this->validation->set_rules($rules);
			$data['alias'] = 'Alias';
			$data['email'] = 'eMail';
			$data['code'] = 'DST';
			$this->validation->set_fields($data);
			
			if ($this->validation->run() == TRUE)
			{
				$rules['register'] = "callback_register_check";
				$this->validation->set_rules($rules) ;
				
				if ($this->validation->run() == FALSE)
				{
					$this->load->view('account/login');
				}
			}else{
				$this->load->library('timetamer');
				$data['dtsform'] = $this->timetamer->MakeForm();
				$data['tpltitle'] = 'Register';
				$data['tplmenu'] = $this->guestMenu();
				$data['captcha'] = $this->captcha_view();
				$this->load->view('account/register', $data);
			}
		}else{
		$this->load->library('timetamer');
		$data['dtsform'] = $this->timetamer->MakeForm();
			$data['tpltitle'] = 'Register';
			$data['tplmenu'] = $this->guestMenu();
			$data['captcha'] = $this->captcha_view();
			$this->load->view('account/register', $data);
		}
	}




	
/*****************************************************************\
|               LOGOUT
\*****************************************************************/	

	function LogOut()
	{
		$this->auth->Logout();
	}
	
	function register_check($str)
	{
		$alias = $this->input->post('alias');
		$email = $this->input->post('email');
		$code = $this->input->post('code');
		$this->load->model('Accountdb');
		$query = $this->Accountdb->Email($email);
		if ($query->num_rows() > 0)
		{
			$this->Account->SendPass($query->alias, $email, $query->pass);
			$this->validation->set_message('register_check', 'Please check your eMail');
			return FALSE;
		}
		else
		{
			$data = $this->Accountdb->Register($alias, $email, $code);
			$this->Account->SendPass($alias, $email, $data->Password);
		}
	}

	
/*****************************************************************\
|               CAPTCHA
\*****************************************************************/	

	function captcha_view()
	{
		$this->load->plugin('captcha');
		$vals = array(
			'img_path'	 => '/var/www/public/captcha/',
			'img_url'	 => 'http://localhost/public/captcha/',
			'font_path'	 => '/var/www/system/fonts/jester.ttf',
			'img_width'	 => 110,
			'img_height' => 25,
			'expiration' => 360
			);
		
		$cap = create_captcha($vals);
		
		$data = array(
			'captcha_id'	=> '',
			'captcha_time'	=> $cap['time'],
			'ip_address'	=> $this->input->ip_address(),
			'word'			=> $cap['word']
			);
		
		$query = $this->db->insert_string('captcha', $data);
		$this->db->query($query);
		
		//$data  = '<br />Submit the word you see below:<br />';
		$data = $cap['image'];
		//$data .= '<br /><input type="text" name="captcha" />';
		return $data;
	}

	
	function captcha_check()
	{
			// First, delete old captchas
			$expiration = time()-1500;
			$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
			
			// Then see if a captcha exists:
			$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? ";
			$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
			$query = $this->db->query($sql, $binds);
			$row = $query->row();
			
			if ($row->count == 0)
			{
				$this->validation->set_message('captcha_check', 'You must submit the word that appears in the image.');
				return FALSE;
			}	
	}
}
?>
